﻿////////////////////////////////////////////////////////////////////////////////
// Source Copyright (C) J.Fan
// @author      J.Fan
// @date        2013-01-19
// @revision    Revision in Version Control System
// @remark      
////////////////////////////////////////////////////////////////////////////////

using System;

namespace JsonConverter.Converter
{
    /// <summary>
    /// convert primitive type value to JSON
    /// </summary>
    public class PrimitiveConverter: IPrimitiveConverter
    {
        public const string TRUE = "true";
        public const string FALSE = "false";

        public string Convert(object value)
        {
            if (!value.GetType().IsPrimitive)
            {
                throw new JsonConverterException("value is not a primitive type",
                    new ArgumentException("value"));
            }

            if (value is bool)
            {
                return ((bool) value) ? TRUE : FALSE;
            }
            else if (value is char)
            {
                // output the char as unicode in number
                return string.Format(@"\u{0:x4}", (int) (char) value);
            }
            else
            {
                return value.ToString();
            }
        }
    }
}
